/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-09-09 21:49:48
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.kb.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.itttq.kb.model.po.TopicPo;

@Mapper
public interface ITopicMapper extends BaseMapper<TopicPo>{
	/**
	  * 获取指定排序的N条数据：
	  * @param field 排序字段名称：update_time - 最新；number_reply - 回复最多；number_read - 查看最多
	  * @param List<TopicPo>
	  * @return
	  */
	@Select({"<script>",
	    "select a.id,a.title,a.category_id,a.category_name,a.is_essence,a.is_recommon,a.is_top,a.number_read,a.number_reply,a.update_time,a.update_time,b.avatar as creator_avatar,a.creator_id,b.`name` as creator_name from kb_topic a,kb_user b ",
	    "where a.creator_id=b.id ",
	    "<when test='categoryId != null'> and a.category_id =#{categoryId} </when>",
	    " order by #{field} desc limit #{size}",
	    "</script>"})
	public List<TopicPo> getTopTopics(@Param("field")String field, @Param("categoryId")Integer catId, @Param("size")int size);
	
	/**
	  * 获取根据更新时间排序的N条数据：
	  * @return
	  */
	@Select({"<script>",
	    "select a.id,a.title,a.category_id,a.category_name,a.is_essence,a.is_recommon,a.is_top,a.number_read,a.number_reply,a.create_time,a.update_time,b.avatar as creator_avatar,a.creator_id,b.`name` as creator_name from kb_topic a,kb_user b ",
	    "where a.creator_id=b.id ",
	    "<when test='title != null'> and a.title like '%#{title}%' </when>",
	    "<when test='isTop != null'> and a.is_top =#{isTop} </when>",
	    "<when test='categoryId != null'> and a.category_id =#{categoryId} </when>",
	    " order by a.update_time desc limit #{start}, #{size}",
	    "</script>"})
	public List<TopicPo> getTopics(@Param("title")String title, @Param("isTop")Integer isTop, @Param("categoryId")Integer catId, 
			@Param("start")int start, @Param("size")int size);
	

	/**
	  * 获取根据更新时间排序的N条数据：
	  * @return
	  */
	@Select("select a.id,a.title,a.category_id,a.category_name,a.is_essence,a.is_recommon,a.is_top,a.number_read,a.number_reply,a.create_time,a.creator_id,a.update_time, a.content,b.`name` as creator_name,b.avatar as creator_avatar from kb_topic a,kb_user b where a.id=#{id} and a.creator_id=b.id")
	public TopicPo getTopic(@Param("id")int id);
}